<?php
/**
 * @package    system
 * @subpackage service
 */

/**
 * Base class for all the services. Defines some common methods.
 * Note that it has a different function than {@link Services}
 */
class Service
{
    /**
     * Used for internal call in each service method.
     * Be called before the real method implementation.
     *
     * @uses
     * <code>
     * class ExampleService extends Service
     * {
     *     public static function exampleServiceMethod($foo,$bar)
     *     {
     *         if (self::_invoke()) return self::_output();
     *
     *         // method implementation
     *     }
     * }
     * </code>
     *
     * @return bool
     */
    protected final static function _invoke()
    {
        return ServiceInvoker::invoke(2);
    }

    /**
     * Bridge to ServiceInvoker::output();
     *
     * @return mixed
     */
    protected final static function _output()
    {
        return ServiceInvoker::output();
    }

    /**
     * Build error from doctrine validator exception
     *
     * @param  Doctrine_Validator_Exception $e
     * @return string
     */
    public static function buildErrorMessageFromDoctrineValidatorException(Doctrine_Validator_Exception $e)
    {
        $errorMsg = '';
        foreach ($e->getInvalidRecords() as $record) {
            foreach($record->getErrorStack() as $fieldName=>$errors) {
                $errorMsg .= "[Model Error] ".$fieldName." :: ".implode(',',$errors);
            }
        }
        return $errorMsg;
    }
}
?>